package edu.xidian.onlinedocument.views.elder;

import edu.xidian.onlinedocument.entity.DocumentData;
import edu.xidian.onlinedocument.handler.excpetions.custom.CustomException;
import edu.xidian.onlinedocument.views.ResultCode;
import edu.xidian.onlinedocument.views.groups.DataGroupA;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.*;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DocumentDataMapper {
    @NotNull
    private int docId;

    @NotBlank
    @Size(min = 2, max = 50, message = "文档名称的长度在2到50个字符之间", groups = DataGroupA.class)
    private String docName;

    @NotNull
    private int docVersion;

    private String userName;

    @Temporal(TemporalType.TIMESTAMP)   //规定Date类型映射为mysql中的DateTime类型
    private Date docCreateTime;

    @Temporal(TemporalType.TIMESTAMP)   //规定Date类型映射为mysql中的DateTime类型
    private Date docUpdateTime;

    @NotNull
    private boolean docIsDelete;

    public DocumentDataMapper(DocumentData docData){
        this.docId = docData.getDocId();
        this.docName = docData.getDocName();
        this.docVersion = docData.getDocVersion();
        this.docCreateTime = docData.getDocCreateTime();
        this.docUpdateTime = docData.getDocUpdateTime();
        this.docIsDelete = docData.getDocIsDelete();
        this.userName = null;
    }

    public DocumentDataMapper(DocumentData docData, String userName){
        this.docId = docData.getDocId();
        this.docName = docData.getDocName();
        this.docVersion = docData.getDocVersion();
        this.docCreateTime = docData.getDocCreateTime();
        this.docUpdateTime = docData.getDocUpdateTime();
        this.docIsDelete = docData.getDocIsDelete();
        this.userName = userName;
    }

    public static DocumentDataMapper turn(DocumentData data){
        if(data.getCreatorData() == null) {
            throw new CustomException(ResultCode.USER_ID_NOT_EXIST);
        }
        return new DocumentDataMapper(data, data.getCreatorData().getUserName());
    }
}
